package com.linrui.org.service.impl;

import com.linrui.basic.service.impl.BaseServiceImpl;
import com.linrui.org.domain.Department;
import com.linrui.org.mapper.DepartmentMapper;
import com.linrui.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;
    @Override
    @Transactional
    public void add(Department department) {
        department.setCreate_time(new Date());
        super.add(department);
        Long id = department.getParent().getId();
        String path = "";
        if ( id == null){
            path = "/" + department.getId();
        }else{
            Department parent = mapper.loadById(id);
            path = parent.getPath() +"/" + department.getId();
        }
        department.setPath(path);
        super.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        department.setUpdate_time(new Date());
        super.update(department);
        Long id = department.getParent().getId();
        String path = "";
        if ( id == null){
            path = "/" + department.getId();
        }else{
            Department parent = mapper.loadById(id);
            path = parent.getPath() +"/" + department.getId();
        }
        department.setPath(path);
        super.update(department);
    }

    @Override
    public List<Department> getTree() {
        return mapper.getTree();
    }

}
